package com.xqb.listen.manager.permission

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import com.xqb.listen.manager.permission.model.MediaType

/**
 * @author Ling
 * @date :2024/11/26 17:06
 */
object PermissionUtil {

    /**
    * 判断是否具有访问本地媒体权限
     * @param context 上下文
     * @param mediaType 媒体类型，详见MediaType枚举类
    * */
    fun hasMediaPermission(context: Context , mediaType: MediaType):Boolean{
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return true
            return ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
        } else {
            return when (mediaType) {
                MediaType.VIDEO -> ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_VIDEO) == PackageManager.PERMISSION_GRANTED
                MediaType.AUDIO -> ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_AUDIO) == PackageManager.PERMISSION_GRANTED
                else -> ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_IMAGES) == PackageManager.PERMISSION_GRANTED
            }
        }
    }

    /**
     * 获取本地媒体权限
     * @param context 上下文
     * @param mediaType 媒体类型，详见MediaType枚举类
     * */
    fun getMediaPermission(context: Context , mediaType: MediaType) : String? {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return null
            return Manifest.permission.READ_EXTERNAL_STORAGE
        } else {
            return when (mediaType) {
                MediaType.VIDEO -> Manifest.permission.READ_MEDIA_VIDEO
                MediaType.AUDIO -> Manifest.permission.READ_MEDIA_AUDIO
                else -> Manifest.permission.READ_MEDIA_IMAGES
            }
        }
    }

    fun hasNotifyPermission(context: Context) : Boolean{
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return true
        return ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
    }

    fun hasManagerExternalStoragePermission(context: Context) : Boolean{
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return true
        return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
            ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
        } else {
            Environment.isExternalStorageManager()
        }
    }

    @RequiresApi(Build.VERSION_CODES.R)
    fun getManagerExternalStoragePermissionIntent(context: Context) : Intent {
        val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
        intent.setData(Uri.parse("package:" + context.packageName))
        return intent
    }

    /**
     * 跳转设置详情页面
     * @param context 上下文
     * @param onResult 跳转回调
     */
    fun skipAppSystemInfoPage(context: Context , onResult : (status : Boolean) -> Unit) {
        try {
            val localIntent = Intent()
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS")
            localIntent.setData(Uri.fromParts("package", context.packageName, null))
            context.startActivity(localIntent)
            onResult(true)
        } catch (e: Exception) {
            onResult(false)
            Log.e("--->", "startAppSettingInfo fail ${e.message}")
        }
    }
}